package main

import (
	"encoding/json"
	"fmt"
)

type JsonTest struct {
	Name    string
	Age     int
	Sex     int8
	Address string
}

func main() {
	//序列化
	jsontest := JsonTest{"tome", 800, 1, "北京市海淀区"}
	marshal, err := json.Marshal(jsontest)
	if err != nil {
		fmt.Println("转换序列化失败")
	}
	fmt.Println(string(marshal))

	//map序列化
	mapInter := make(map[string]interface{})
	mapInter["name"] = "tome"
	mapInter["age"] = 23
	mapInter["sex"] = int8(23)
	bytes, err := json.Marshal(mapInter)
	if err != nil {
		return
	}
	fmt.Println(string(bytes))

	//slice序列化
	seliceInterDemo := make([]map[string]interface{}, 10)
	seliceInterDemo = append(seliceInterDemo, map[string]interface{}{
		"name": "name",
		"age":  "age",
	})
	seliceInterDemo = append(seliceInterDemo, map[string]interface{}{
		"sex":     "sex",
		"address": "address",
		"array":   [2]string{"beijing", "shanghai"},
	})
	bytes, err = json.Marshal(seliceInterDemo)
	if err != nil {
		return
	}
	fmt.Println(string(bytes))

	//基本数据类型序列化
	fl := float64(1.1)
	v, _ := json.Marshal(fl)
	fmt.Println(string(v))
}
